<?php
require_once(dirname(__FILE__)."/../config/config.php");
 /**
  * 店铺添加收藏
  * */
  if($_REQUEST['method']=="addFavoShop"||$_REQUEST['method']=="unFavoShop"){
	  	$user_id=$_SESSION['_user']['user_id'];
	  	$shop_id=$_REQUEST['shop_id'];  	
	  	$user_name=$_SESSION['_user']['username'];
	  if($_REQUEST['method']=="addFavoShop"){
	  	mysql_query("INSERT INTO `zy_favoshop` (`user_id`,`shop_id`,`is_delete`,`create_user`,`create_time`,`update_user`,`update_time`) VALUES ('$user_id','$shop_id','0','$user_name',now(),'$user_name',now())");
	  }  
	  else if($_REQUEST['method']=="unFavoShop"){
	  	mysql_query("delete from zy_favoshop where user_id=$user_id and shop_id=$shop_id");
	  }
  }
  /**
   * 添加购物车
   */
  if($_REQUEST['method']=="addcart"){
  	$b=false;
  	if($_SESSION['_cart']['shop_id']!=$_REQUEST['shop_id']&&isset($_SESSION['_cart']['shop_id'])){
		echo json_encode($b);
		return ;
	}
	if(isset($_SESSION['_user'])){
		$_SESSION['_cart']['user_id']=$_SESSION['_user']['user_id'];
	}
	if(!isset($_SESSION['_cart']['shop_id'])){
		$_SESSION['_cart']['shop_id']=$_REQUEST['shop_id'];
	}
	if(!isset($_SESSION['_cart']['shop_name'])){
		$_SESSION['_cart']['shop_name']=$_REQUEST['shop_name'];
	}
	if(count($_SESSION['_cart']['food'])==0){
		$_SESSION['_cart']['food'][count($_SESSION['_cart']['food'])]=array("food_id"=>$_REQUEST['food_id'],"food_name"=>$_REQUEST['food_name'],"quantity"=>$_REQUEST['quantity'],"price"=>$_REQUEST['price']);
	}else{
		foreach ($_SESSION['_cart']['food'] as $food) {
		   if($food['food_id']==$_REQUEST['food_id']){
			   	echo json_encode("add_quantity");
				return ;
		   }
		}
		$_SESSION['_cart']['food'][count($_SESSION['_cart']['food'])]=array("food_id"=>$_REQUEST['food_id'],"food_name"=>$_REQUEST['food_name'],"quantity"=>$_REQUEST['quantity'],"price"=>$_REQUEST['price']);
	}
		$result=mysql_query("select * from zy_food where food_id=".$_REQUEST['food_id']);
		if($result=mysql_fetch_array($result)){
			$out=outputCartOne($result,$_REQUEST['quantity']);
			echo json_encode($out);
		}else{
			echo json_encode(null);
		}
  }else if($_REQUEST['method']=="updatecart"){
  		for($i=0;$i<count($_SESSION['_cart']['food']);$i++){
  			if($_SESSION['_cart']['food'][$i]['food_id']==$_REQUEST['food_id']){
  				$_SESSION['_cart']['food'][$i]['quantity']=$_REQUEST['quantity'];
  				echo json_encode("success");
  				//print_r($_SESSION['_cart']);
  				return ;
  			}
  		}
  }
  else if($_REQUEST['method']=="deleteOne"){
  		for($i=0;$i<count($_SESSION['_cart']['food']);$i++){
  			if($_SESSION['_cart']['food'][$i]['food_id']==$_REQUEST['food_id']){
  				if(count($_SESSION['_cart']['food'])==1){
  					unset($_SESSION['_cart']);
  					echo json_encode("none");
  				}else{
  					unset($_SESSION['_cart']['food'][$i]);
  					sort($_SESSION['_cart']['food']);
  					echo json_encode("success");
  				}
  				//print_r($_SESSION['_cart']);
  				return ;
  			}
  		}
  }
  /*
   * 根据ID查询下级商圈.
   */
  else if($_REQUEST['method']=="findArea"){
  		$sql="select * from zy_businessarea where parentId=".$_REQUEST['id'];

  		$result=mysql_query($sql);
  		$count=mysql_num_rows($result);
  		$output="<div id=\"sub-area\"><ul><span style=\"float:left; margin-left:-80px; padding:3px 0; font-size:12px;\">请继续选择-&gt;</span>";
  		if($count>0){
  			while($arr=mysql_fetch_array($result)){
  				$output.="<li><a href=\"".SITE_ROOT."/index.php?bid=".$arr['id']."\">".$arr['name']."</a></li>";
  			}
  		}else{
  			$output.="<li>该区域暂未开通.</li>";
  		}
  		
  		$output.="</ul></div>";
  		echo json_encode($output);
  }
  /**
   * 单品评价
   */
  else if($_REQUEST['method']=="foodreview"){
  	$food_id=$_REQUEST['food_id'];
  	$order_id=$_REQUEST['order_id'];
  	$user_id=$_SESSION['_user']['user_id'];
  	$food_score=$_REQUEST['food_score'];
  	$sql="INSERT INTO `zy_food_review`(`order_id`,`food_id`,`food_score`,`food_review_content`) VALUES ('$order_id','$food_id','$food_score','')";
 	mysql_query($sql);
  }
   /**
  * 单品添加收藏
  * */
  if($_REQUEST['method']=="addFavoFood"||$_REQUEST['method']=="unFavoFood"){
	  	$user_id=$_SESSION['_user']['user_id'];
	  	$food_id=$_REQUEST['food_id'];  	
	  	$user_name=$_SESSION['_user']['username'];
	  if($_REQUEST['method']=="addFavoFood"){
	  	mysql_query("INSERT INTO `zy_favofood` (`user_id`,`food_id`,`is_delete`,`create_user`,`create_time`,`update_user`,`update_time`) VALUES ('$user_id','$food_id','0','$user_name',now(),'$user_name',now())");
	  }  
	  else if($_REQUEST['method']=="unFavoFood"){
	  	mysql_query("delete from zy_favofood where user_id=$user_id and food_id=$food_id");
	  }
  }
/**
 * 计算购物车总计
 */   
function totalPrice(){
	$total_price=0;
	if(count($_SESSION['_cart']['food'])>0){
		foreach($_SESSION['_cart']['food'] as $food){
			$price=$food['quantity']*$food['price'];
			$total_price+=$price;
		}
	}
	return $total_price;
}
/**
 * 输出购物车里单条商品信息
 */
function outputCartOne($result,$quantity){
	$detail="<tr class=\"food-line cart-food-rows\" id=\"bottom-cart-food-".$result['food_id']."\" rel=\"".$result['food_id']."\">";
	$detail.="<td class=\"name\"><span>".$result['food_name']."</span></td>";
	$detail.="<td class=\"price\">￥".$result['price']."</td>";
	$detail.="<td class=\"quantity\"><a href=\"javascript:cart_reduce_quantity(".$result['food_id'].");\" class=\"cart-action cart-decrease\"></a>";
	$detail.="<input class=\"order-item  subquantity totalquantity\"  type=\"text\" autocomplete=\"off\" value=\"".$quantity."\" onkeyup=\"javascript:only_number($(this),".$result['food_id'].")\">";
	$detail.="<a href=\"javascript:cart_add_quantity(".$result['food_id'].");\" class=\"cart-action cart-increase\"></a></td>";
	$detail.="<td class=\"subtotal cart-subtotal\"  price=\"".$result['price']."\">￥".$result['price']*$quantity."</td>";
	$detail.="<td class=\"action\"><a href=\"javascript:cart_delete_one(".$result['food_id'].");\" class=\"cart-action cart-delete\"> </a></td>";
	$detail.="</tr>";
	return $detail;
}
/**
 * 输出整个购物车信息
 */
function outputAllCartDetails(){
	$out='';
	if(count($_SESSION['_cart']['food'])>0){
		foreach($_SESSION['_cart']['food'] as $food){
			$content=outputCartOne($food,$food['quantity']);
			$out.=$content;
		}
	}
		$shop_id = $_REQUEST['shop_id'];
		if($shop_id>0){
		$sql="select * from zy_shop where shop_id =".$shop_id;
		}else{
		$sql="select * from zy_shop where shop_id =".$_SESSION['_cart']['shop_id'];
		}
		//echo $sql;
		$result=mysql_query($sql);
		$zy_shop=mysql_fetch_array($result);
		$out.="<tr class=\"food-line cart-food-rows\" id=\"bottom-cart-food-trade\">";
		$out.="<td class=\"name\"><span>外送费</span></td>";
		$out.="<td class=\"price\"></td>";
		$out.="<td class=\"quantity\">";
		$out.="</td>";
		$out.="<td class=\"subtotal cart-subtotal\">￥{$zy_shop['surcharge']}</td>";
		$out.="<td class=\"action\"></td>";
		$out.="</tr>";
	
	return $out;
}
/**
 * 输出购物车总计信息
 */
function outputCartTotal(){
	$total_quantity=0;
	if(count($_SESSION['_cart']['food'])>0){
		foreach($_SESSION['_cart']['food'] as $food){
			$total_quantity+=$food['quantity'];
		}
		$sql="select * from zy_shop where shop_id =".$_SESSION['_cart']['shop_id'];
		$result=mysql_query($sql);
		$zy_shop=mysql_fetch_array($result);
		echo "共&nbsp;<strong class=\"total-quantity_2\" id=\"total_quantity\">".$total_quantity."</strong>&nbsp;份美食&nbsp;|&nbsp;合计&nbsp;<strong class=\"total-price\" id=\"total_price_2\">".(totalPrice()+$zy_shop['surcharge'])."</strong>&nbsp;元 ";
	}
}
/**
 * 输出点餐车的总数
 */
function outputCartTotals(){
	$total_quantity=0;
	if(count($_SESSION['_cart']['food'])>0){
		foreach($_SESSION['_cart']['food'] as $food){
			$total_quantity+=$food['quantity'];
		}
	}
	if($total_quantity == 0)
	{
		echo "空";
	}else
	{
		echo $total_quantity;
	}
}



?>
